<template>
	<div class="item-enterprise">
		<ul class="ul-enterprise">
			<li class="li-enterprise" v-for="item in items">
				<div class="enterprise" @click="go(item, item.enterpriseId, '0')">
					<dl class="flex-box">
						<dd class="flex-item"><i class="icon-enterpriseAvatar"></i></dd>
						<dd class="flex-item">{{item.enterpriseName}}</dd>
						<dd class="flex-item"><i class="icon-arrowRight"></i></dd>
					</dl>
				</div>
				<ul class="ul-department" v-if="item.departmentListOfUser">
					<li class="li-department" v-for="dp in item.departmentListOfUser[0]" @click="go(dp, item.enterpriseId, dp.departmentId)">
						<dl class="flex-box">
							<dd class="flex-item"><i class="icon-lowerLevel"></i></dd>
							<dd class="flex-item">{{dp.name}}</dd>
							<dd class="flex-item"><i class="icon-arrowRight"></i></dd>
						</dl>
					</li>
				</ul>
			</li>
		</ul>
	</div>
</template>

<script>
export default {

  	name: 'index',

  	props: {
  		items: [Object, Array]
  	},

  	data () {
	    return {

	    };
  	},

  	methods: {
  		go (item, enterpriseId, departmentId) {
  			this.setDefaultEnterprise(enterpriseId).then(
  				res => {
		  			this.$router.push({
		  				name: 'list', 
		  				params: {
		  					departmentId: departmentId
		  				}
		  			});	
  				}
  			);
  		},

  		/**
  		 * 切换默认登录企业
  		 * @param {String} enterpriseId  企业ID
  		 */
  		setDefaultEnterprise (enterpriseId) {
  			return new Promise((resolve, reject) => {
  				this.$http.post(this.api.eabcommon, {
  					action: 'setDefaultEnterpriseId',
  					enterpriseId: enterpriseId
  				}, {showLoading: true}).then(
  					res => {
  						if (res.body.result === 1) {

  							let isSuperAdministrator = this.cache.roleIds.contains('1');
  							if (isSuperAdministrator) {

  								let enterprises = this.cache.enterprises;
								for (let i=0; i<enterprises.length; i++) {
									let enterprise = enterprises[i];
									if (enterpriseId == enterprise.enterpriseId) {
										this.cache.defaultEnterprise = enterprise;
									}
								}
	  							resolve(isSuperAdministrator);
  							} else {
  								// 无超级管理员权限是重新切回上一登录企业
  								this.$http.post(this.api.eabcommon, {
  									action: 'setDefaultEnterpriseId',
  									enterpriseId: this.cache.defaultEnterprise.enterpriseId
  								}).then(
  									res => {
  										this.$router.push({name: 'permission_limit'});
  									}
  								);
  							}
  						}
  					}
  				);
  			});
  		}
  	}
};
</script>

<style lang="scss" scoped>
	@import '../../scss/mixin.scss';
	.item-enterprise{
		.ul-enterprise{
			.li-enterprise{
				background: #fff;
				margin-bottom: 20px;
				.enterprise{
					height: 100px;
					line-height: 100px;
					font-size: 28px;
					&:active{
						background: rgb(243, 243, 243);
					}
				}
				
			}
		}
		.ul-department{
			.li-department{
				height: 100px;
				line-height: 100px;
				font-size: 28px;
				&:last-child{
					dd{
						&:after{
							border: none !important;
						}
					}
				}
				dd:first-child{
					&:after{
						border: none !important;
					}
				}
				&:active{
					background: rgb(243, 243, 243);
				}
			}

		}
	}
	
	
</style>